/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * An abstract class extending JDialog representing a dialog on screen.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public class NodeSpringDialog extends SimpleDialog {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	private String gAnlType;
	
	private DecimalField KxField;
	private DecimalField KyField;
	private DecimalField KzField;
	private DecimalField KrxField;
	private DecimalField KryField;
	private DecimalField KrzField;
	public double Kx;
	public double Ky;
	public double Kz;
	public double Krx;
	public double Kry;
	public double Krz;
	
//*****************************************************************************
	
	public NodeSpringDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		gAnlType = parent.getModel().getDiscreteModel().getGlobalAnalysisModel().getType();
		
		setUpFormat();
		setUpFields();
		
		JPanel springPane = new JPanel();
		springPane.setPreferredSize(new Dimension(360, 280));
		springPane.setLayout(new GridLayout(4,1,2,2));
		JPanel springDPane = new JPanel();
		springDPane.setLayout(new GridLayout(1,3,2,2));
		springPane.add(springDPane);
		JPanel springValueDPane = new JPanel();
		springValueDPane.setLayout(new GridLayout(1,3,2,2));
		springPane.add(springValueDPane);
		JPanel springRPane = new JPanel();
		springRPane.setLayout(new GridLayout(1,3,2,2));
		springPane.add(springRPane);
		JPanel springValueRPane = new JPanel();
		springValueRPane.setLayout(new GridLayout(1,3,2,2));
		springPane.add(springValueRPane);
		
		springDPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/SpringKx64.gif")) ));
		springDPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/SpringKy64.gif")) ));
		springDPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/SpringKz64.gif")) ));
		springRPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/SpringKrx64.gif")) ));
		springRPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/SpringKry64.gif")) ));
		springRPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/SpringKrz64.gif")) ));
		
		JPanel springKxPane = new JPanel();
		JPanel springKyPane = new JPanel();
		JPanel springKzPane = new JPanel();
		JPanel springKrxPane = new JPanel();
		JPanel springKryPane = new JPanel();
		JPanel springKrzPane = new JPanel();
		
		springKxPane.add(new JLabel("Kx:"));
		springKyPane.add(new JLabel("Ky:"));
		springKzPane.add(new JLabel("Kz:"));
		springKrxPane.add(new JLabel("Krx:"));
		springKryPane.add(new JLabel("Kry:"));
		springKrzPane.add(new JLabel("Krz:"));
		
		springKxPane.add(KxField);
		springKyPane.add(KyField);
		springKzPane.add(KzField);
		springKrxPane.add(KrxField);
		springKryPane.add(KryField);
		springKrzPane.add(KrzField);
		
		springValueDPane.add(springKxPane);
		springValueDPane.add(springKyPane);
		springValueDPane.add(springKzPane);
		springValueRPane.add(springKrxPane);
		springValueRPane.add(springKryPane);
		springValueRPane.add(springKrzPane);
		
		if (gAnlType.equals("PlaneFrame")) {
			KzField.setEnabled(false);
			KrxField.setEnabled(false);
			KryField.setEnabled(false);
		}
		else if (gAnlType.equals("PlaneTruss")) {
			KzField.setEnabled(false);
			KrxField.setEnabled(false);
			KryField.setEnabled(false);
			KrzField.setEnabled(false);
		}
		else if (gAnlType.equals("Beam")) {
			KxField.setEnabled(false);
			KzField.setEnabled(false);
			KrxField.setEnabled(false);
			KryField.setEnabled(false);
		}
		else if (gAnlType.equals("Grid")) {
			KxField.setEnabled(false);
			KzField.setEnabled(false);
			KryField.setEnabled(false);
		}
		
		setPane(springPane);
	}
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(this.parent, parent.getResourceBundle().getString("dNSprDialog01"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		try {
			Kx = KxField.getValue();
			Ky = KyField.getValue();
			Kz = KzField.getValue();
			Krx = KrxField.getValue();
			Kry = KryField.getValue();
			Krz = KrzField.getValue();
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
//*****************************************************************************
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		if (this.parent.getController().getSelection().size() == 1 ) {
			Node node = (Node)this.parent.getController().getSelection().get(0);
			
			KxField = new DecimalField(node.getSpring(0), 7, numberFormat);
			KyField = new DecimalField(node.getSpring(1), 7, numberFormat);
			KzField = new DecimalField(node.getSpring(2), 7, numberFormat);
			KrxField = new DecimalField(node.getSpring(3), 7, numberFormat);
			KryField = new DecimalField(node.getSpring(4), 7, numberFormat);
			KrzField = new DecimalField(node.getSpring(5), 7, numberFormat);
			
			if (node.getRestraint(0) || node.getAngle() != 0)
				KxField.setEnabled(false);
			if (node.getRestraint(1) || node.getAngle() != 0)
				KyField.setEnabled(false);
			if (node.getRestraint(2) || node.getAngle() != 0)
				KzField.setEnabled(false);
			if (node.getRestraint(3) || node.getAngle() != 0)
				KrxField.setEnabled(false);
			if (node.getRestraint(4) || node.getAngle() != 0)
				KryField.setEnabled(false);
			if (node.getRestraint(5) || node.getAngle() != 0)
				KrzField.setEnabled(false);
			
		} else {
			
			KxField = new DecimalField(0, 7, numberFormat);
			KyField = new DecimalField(0, 7, numberFormat);
			KzField = new DecimalField(0, 7, numberFormat);
			KrxField = new DecimalField(0, 7, numberFormat);
			KryField = new DecimalField(0, 7, numberFormat);
			KrzField = new DecimalField(0, 7, numberFormat);
			
		}
	}
	
//*****************************************************************************
}
